package com.tools.gensql.controller;

import com.tools.gensql.model.ColumnSearchResultDTO;
import com.tools.gensql.model.ConnectionVO;
import com.tools.gensql.model.ExecuteQueryDTO;
import com.tools.gensql.model.GenerateSqlDTO;
import com.tools.gensql.model.LlmVO;
import com.tools.gensql.model.QueryResultDTO;
import com.tools.gensql.model.R;
import com.tools.gensql.model.TableVO;
import com.tools.gensql.service.AppService;
import org.noear.solon.annotation.Body;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Delete;
import org.noear.solon.annotation.Get;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Param;
import org.noear.solon.annotation.Post;
import org.noear.solon.core.handle.Context;
import org.noear.solon.web.cors.annotation.CrossOrigin;

import java.util.List;

@Controller
@CrossOrigin(origins = "*")
public class AppController {

    @Inject
    private AppService appService;

    @Mapping("/")
    public void index(Context ctx) {
        ctx.forward("/index.html");
    }

    @Mapping("/getConnections")
    @Get
    public R<List<ConnectionVO>> getConnections() {
        return R.from(() -> appService.getConnections());
    }

    @Mapping("/getConnection/{id}")
    @Get
    public R<ConnectionVO> getConnection(@Param("id") Long id) {
        return R.from(() -> appService.getConnection(id));
    }

    @Mapping("/getTables")
    @Get
    public R<List<TableVO>> getTables(@Param("connectionId") Long connectionId) {
        return R.from(() -> appService.getTables(connectionId));
    }

    @Mapping("/saveConnection")
    @Post
    public R<String> saveConnection(@Body ConnectionVO connectionVO) {
        return R.from(() -> appService.saveConnection(connectionVO));
    }
    
    @Mapping("/deleteConnection/{id}")
    @Delete
    public R<String> deleteConnection(@Param("id") Long id) {
        return R.from(() -> appService.deleteConnection(id));
    }

    @Mapping("/generateSql")
    @Post
    public R<String> generateSql(@Body GenerateSqlDTO generateSqlDTO) {
        return R.from(() -> appService.generateSql(generateSqlDTO));
    }

    @Mapping("/connection")
    @Get
    public R<String> connection(@Param("connectionId") Long connectionId) {
        return R.from(() -> appService.connection(connectionId));
    }

    @Mapping("/testConnection")
    @Post
    public R<String> testConnection(@Body ConnectionVO connectionVO) {
        return R.from(() -> appService.testConnection(connectionVO));
    }

    @Mapping("/searchColumns")
    @Get
    public R<List<ColumnSearchResultDTO>> searchColumns(@Param("connectionId") Long connectionId, @Param("keyword") String keyword) {
        return R.from(() -> appService.searchColumns(connectionId, keyword));
    }

    @Mapping("/getModelSetting")
    @Get
    public R<LlmVO> getModelSetting() {
        return R.from(() -> appService.getModelSetting());
    }

    @Mapping("/saveModelSetting")
    @Post
    public R<String> saveModelSetting(@Body LlmVO llmVO) {
        return R.from(() -> appService.saveModelSetting(llmVO));
    }

    @Mapping("/executeQuery")
    @Post
    public R<QueryResultDTO> executeQuery(@Body ExecuteQueryDTO executeQueryDTO) {
        return R.from(() -> appService.executeQuery(executeQueryDTO));
    }
}